
    $(function () {
    var username//用户名
    var listPages=0//表示要分多少页
    var page//当前页数
    var listJson;//主页json对象，用来存储登录用户的信息
    var queryString = window.location.search;
    var params = new URLSearchParams(queryString);
    username = params.get("username");
    page = params.get("page")||1;
    //定义两个数组用来存放文章的html代码
    var passagesHtml = [], commentsHtml = [];
    $.ajax({
    url: "/blogSystem/list?username=" + username + "&page=" + page,
    dataType: "json",
    success: function (json) {
    //从后端接收该用户的文章信息需要分的页数
    listPages = json.listPage
    //接收该用户的文章信息
    listJson = json.articles
    for (var i = 0; i < json.articles.length; i++) {
    passagesHtml.push(json.articles[i])
}
    for (var i = 0; i < json.articles.length; i++) {
    commentsHtml.push(json.articles[i])
}
    $("#author").html(username)
    $.each(passagesHtml, function (index, item) {
    var blogHtml = `
                            <div class="blog">
                                <div class="title">${item.title}</div>
                                <div class="date">${new Date(item.publishedTime).toLocaleDateString()}</div>
                                <div class="desc">${item.content.substring(0, 100)}...</div>
                                <a href="javascript:void(0)" id="detailDisplay${item.id}" class="detail">
                                    查看全文&gt;&gt;
                                </a>
                            </div>
                        `;
    $("body").on("click", "#detailDisplay" + item.id + "", function () {
    window.location="/blogSystem/blog/blog_detail.html?articleId="+item.id+"&username="+username
})
    // 将新创建的博客元素添加到容器中
    $(".blog-list").append(blogHtml);

});
    $("#currentPage").html(page)
    $("#totalPage").html(listPages)
},
    error: function () {
    alert("用户信息获取失败")
}

})
    $("#shuanxin").click(function (){
    window.location.href="/blogSystem/blog/blog_list.html?username="+username+"&page="+page;
})
    $("#mainPage").click(function (){
    window.location='blog_list.html?username=' + username + '&page=' + page
})

    $("#editBlog").click(function (){
    window.location='blog_edit.html?username=' + username + '&page=' + page
})
    $("#square").click(function (){
    window.location='blog_square.html?username='+username+ '&page=' + page
})
    // 分页事件绑定
    //点击上一页的事件
    $('.prev').on('click', function () {

    var currentPage = parseInt($('#currentPage').text()) - 1;
    if (currentPage > 0) {
    window.location.href = 'blog_list.html?username=' + username + '&page=' + currentPage
} else if (currentPage <= 0) {
    alert("已是第一页")
}
});
    //点击下一页的事件
    $('.next').on('click', function () {

    var currentPage = parseInt($('#currentPage').text()) + 1;
    if (currentPage > listPages) {
    alert("已是最后一页")
} else {
    window.location.href = 'blog_list.html?username=' + username + '&page=' + currentPage
}
});
})

